package com.cms.controller.ServerConfig;

import com.cms.common.JsonResult;
import com.jfinal.aop.Before;
import com.jfinal.core.Controller;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;
import com.jfinal.plugin.activerecord.tx.Tx;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

//服务器配置
public class ServerConfigController extends Controller{

    public void index(){
        renderJsp("/pages/ServerConfig/serverConfig_add.jsp");
    }

    @Before(Tx.class)
    public void save(){
        String serverName = getPara("serverName");
        String port = getPara("port");
        String env = getPara("env");
        String ip = getPara("ip");

        Record server = new Record();
        server.set("ip",ip).set("port",port).set("env",env).set("server_name",serverName).set("create_time",new Date()).set("update_time",new Date());
        Db.save("server_manage",server);

        renderJson(new JsonResult().status("000").show("保存成功"));
    }

    @Before(Tx.class)
    public void serverEditToSave(){
        String id = getPara("id");
        String serverName = getPara("serverName");
        String port = getPara("port");
        String env = getPara("env");
        String ip = getPara("ip");

        Record server = Db.findFirst("SELECT * FROM server_manage WHERE id = ?", id);

        if(null!=server){
            server.set("ip",ip).set("port",port).set("env",env).set("server_name",serverName).set("update_time",new Date());
            Db.update("server_manage",server);
        }

        renderJson(new JsonResult().status("000").show("保存成功"));
    }

    public void pagination(){

        int pageNumber = getParaToInt("pageNumber");
        int pageSize = getParaToInt("pageSize");

        Page<Record> recordList = Db.paginate(pageNumber, pageSize, "SELECT *", " FROM server_manage");

        Map<String, Object> returnMap = new HashMap<String, Object>();
        returnMap.put("total", recordList.getTotalRow());
        returnMap.put("rows", recordList.getList());
        renderJson(returnMap);
    }

    public void edit(){

        String server_id = getPara("id");
        Record record = Db.findFirst("SELECT * FROM server_manage WHERE id = ?", server_id);
        if(record != null){
            setAttr("server",record);
        }

        renderJsp("/pages/ServerConfig/serverConfig_edit.jsp");
    }

    @Before(Tx.class)
    public void delete() {

        String server_id = getPara("server_id");
        Record server = Db.findById("server_manage", server_id);

        if(server!=null){
            Db.update("delete from server_manage where id = ?",server_id);
        }
        renderJson(new JsonResult().status("000").show("删除成功"));
    }

    @Before(Tx.class)
    public void serverStop(){
        String server_id = getPara("server_id");
        Record server = Db.findById("server_manage", server_id);

        if(server!=null){
            server.set("status",0).set("update_time",new Date());
            Db.update("server_manage",server);
        }
        renderJson(new JsonResult().status("000").show("停止成功"));
    }

    @Before(Tx.class)
    public void serverStart(){
        String server_id = getPara("server_id");
        Record server = Db.findById("server_manage", server_id);

        if(server!=null&&server.getInt("status")==0){
            server.set("status",1).set("update_time",new Date());
            Db.update("server_manage",server);
        }
        renderJson(new JsonResult().status("000").show("启动成功"));
    }
}
